package StudyClass;
import java.util.*;
public class test
{
    public static void main(String[] args)
    {
        init();

    }
    public static void init()
    {
        List<PCB> list=new ArrayList<>();
        list.add(new PCB("P0",0.0,5.0,0,null,5.0,null,null,null,"Finish",null));
        list.add(new PCB("P1",3.0,4.0,5,null,3.0,null,null,null,"Finish",null));
        list.add(new PCB("P2",6.0,3.0,9,null,9.0,null,null,null,"Finish",null));
        CalendarorPriority(list);
        CalendarorTurnaroundTime(list);
        CalendarorWeightTurnaroundTime(list);
        CalendarorHpf(list);
        FCFS(list);
        SPF(list);
        HPF(list);
        HPF2(list);
        RR(list);

    }

    private static void RR(List<PCB> list)
    {
        //找到list对象中servertime最小的索引
        double min=9999;
        for(PCB pcb:list)
        {
            if(pcb.getServiceTime()<min)
            {
                min=pcb.getServiceTime();
            }
        }
        while(true)
        {
            Random r=new Random();
            int Index=r.nextInt(list.size());
            if(Index!=min)
            {
                //System.out.println(Index+"INDEX");
                double time=list.get(Index).getServiceTime();
                for(int i=0;i<5;i++)
                {
                    for(PCB pcb:list)
                    {
                        if(pcb.getServiceTime()<=time)
                        {
                            //System.out.println("小于我的时间片，执行顺序不用管");
                        }
                        else
                        {
                            operator(pcb,list,time);
                        }
                    }
                }
                break;
            }
        }
      printff(list);



    }
//把头对象滞后
    public static void operator(PCB pcb,List<PCB>list,double time)
{
    pcb.setServiceTime(pcb.getServiceTime()-time);
    PCB TEMP = new PCB();
    TEMP=pcb;
    list.remove(pcb);
    list.add(TEMP);
}
    private static void HPF2(List<PCB> list)
    {
        list.sort(new Comparator<PCB>()//相应比降序
        {
            @Override
            public int compare(PCB o1, PCB o2) {
                return Double.compare(o2.getHpf(),o1.getHpf());//这个方法就是帮你0.5不会因为转化问题而转化为0//因为这个方法是默认返回int
            }
        });
        printff(list);
    }



    private static void HPF(List<PCB> list)
  {
      list.sort(new Comparator<PCB>()//优先级降序
      {
          @Override
          public int compare(PCB o1, PCB o2) {
              return Double.compare(o2.getPriority(),o1.getPriority());//这个方法就是帮你0.5不会因为转化问题而转化为0//因为这个方法是默认返回int
          }
      });
      printff(list);
  }

    private static void SPF(List<PCB> list)//执行时间升序
    {
        list.sort(new Comparator<PCB>()
        {
            @Override
            public int compare(PCB o1, PCB o2) {
                return Double.compare(o1.getServiceTime(),o2.getServiceTime());//这个方法就是帮你0.5不会因为转化问题而转化为0//因为这个方法是默认返回int
            }
        });
        printff(list);
    }
    private static void CalendarorHpf(List<PCB> list)
    {
        for(PCB pcb:list)
        {
            pcb.setHpf(1.0+(pcb.getStartTime()-pcb.getArrivalTime()/pcb.getServiceTime()));
        }


    }

    private static void CalendarorWeightTurnaroundTime(List<PCB> list)
    {
        for(PCB pcb : list)
        {
            pcb.setWeightTrunaroundTime(pcb.getTurnaroundTime()/pcb.getServiceTime());
        }
    }

    private static void CalendarorTurnaroundTime(List<PCB> list)//周转时间没问题
    {
        for(PCB pcb : list)
        {
            pcb.setTurnaroundTime(pcb.getStartTime()+pcb.getServiceTime());
        }
    }


    private static void CalendarorPriority(List<PCB>list)//
    {//    我pcb就是list.get（index）本身
        list.get(0).setPriority(1.0+(list.get(0).getArrivalTime()/list.get(0).getServiceTime()));
        list.get(1).setPriority(1.0+(list.get(1).getArrivalTime()/list.get(1).getServiceTime()));
        list.get(2).setPriority(1.0+(list.get(2).getArrivalTime()/list.get(2).getServiceTime()));
    }

    public static void FCFS(List<PCB> list)//先来先服务
    {
        list.sort(new Comparator<PCB>() {
            @Override
            public int compare(PCB o1, PCB o2) {
                return Double.compare(o1.getArrivalTime(),o2.getArrivalTime());//这是个方法，要传两个参数这个方法就是帮你0.5不会因为转化问题而转化为0//因为这个方法是默认返回int
            }
        });
       printff(list);
    }
    public static void printff(List<PCB> list)
    {


        System.out.println("进程运行顺序如下");
        for (PCB pcb : list)
        {
            System.out.println(pcb);
        }
    }
}
